package com.kgc.dao;

import com.kgc.entity.Cost;
import com.kgc.utils.PageUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CostDao extends BaseDao {

    public List<Cost> selcetAllbytype(PageUtils pageUtils, String type) {
        List<Cost> list = new ArrayList<>();
        String sql = "SELECT up.id,p.`name`,u.username,up.`value`,up.time,up.`status` from user_payment up INNER JOIN  user u on up.user_id=u.id " +
                "INNER JOIN payment p on up.payment_id=p.id ";

        if (type != null && !"".equals(type)) {
            sql += "where up.status =" + type;
        }
        sql += " ORDER BY up.id limit ?,? ";
        try {
            ResultSet rs = super.getExecuteQuery(sql, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new Cost(rs.getInt(1), rs.getString(2), rs.getString(3),
                        rs.getString(4), rs.getDate(5), rs.getInt(6)));
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;

    }

    public int count() {
        String sql = "select count(*) from user_payment";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }


    public int jiaofei(Integer id) {
        String sql = "UPDATE `user_payment` SET  `time` = (SELECT now()), `status` = 1 WHERE `id` = ?;";
        return super.getExecuteUpdate(sql, id);

    }

    public List<Cost> selcetAll(PageUtils pageUtils) {

        List<Cost> list = new ArrayList<>();
        String sql = "SELECT up.id,p.`name`,u.username,up.`value`,up.time,up.`status` from user_payment up INNER JOIN  user u on up.user_id=u.id " +
                "INNER JOIN payment p on up.payment_id=p.id ";
        sql += " ORDER BY up.id limit ?,? ";
        try {
            ResultSet rs = super.getExecuteQuery(sql, pageUtils.start(), pageUtils.getPageSize());
            while (rs.next()) {
                list.add(new Cost(rs.getInt(1), rs.getString(2), rs.getString(3),
                        rs.getString(4), rs.getDate(5), rs.getInt(6)));
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;

    }

    public Integer setPay(String id, String type, String money) {
        Integer index = -1;
        String sql1 = "SELECT `status` FROM user_payment WHERE user_id = ? and payment_id = ?";
        try {
            ResultSet rs = super.getExecuteQuery(sql1, id, type);
            if (rs.next()) {
                index = rs.getInt(1);
            }
            if (index == 0) {
                String sql4 = "UPDATE `user_payment` SET  `value` = ? WHERE user_id = ? and payment_id = ?";
                return super.getExecuteUpdate(sql4, money, id, type);
            } else {
                String sql2 = "DELETE from `user_payment` WHERE user_id = ? and payment_id = ? ";
                super.getExecuteUpdate(sql2, id, type);
                String sql3 = "INSERT INTO `user_payment`(`id`, `user_id`, `payment_id`, `value`, `time`, `status`) VALUES (null, ?, ?, ?, NULL, 0);";
                return super.getExecuteUpdate(sql3, id, type, money);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;

    }
}
